<?php


namespace app\common\controller;
use app\common\exception\BaseException;
use common;
use RequestContext;
use think\Controller;


/**
 * Class BaseController
 * @package app\common\controller
 */
class BaseController extends Controller
{
    var $robot_key = null;
    var $user_info = [
        'id' => 'test0001',
        'realname' => 'kitty',
    ];
    var $id = null;
    protected $resp;
    protected $jsdata;
    protected $respbuf = null;
    protected $time = null;
    protected $sn = null;
    protected $paramlist = null;
    protected $argv = null;
    protected $city_id = null;
    protected $ip = null;

    /**
     *
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->time = explode(" ",microtime());

        $this->resp['status'] = 0;
        RequestContext::$sn = common::sn();
    }

    /**
     * @param $code
     * @param $msg
     * @throws BaseException
     *//**
     * @param $code
     * @param string $msg
     * @throws BaseException
     */
    protected function throw_myexception($err_code, $msg = '') {
        throw new BaseException(['errorCode'=>$err_code, 'msg'=>$msg]);
    }

    //返回数据
    protected function ret_resp()
    {
        return json($this->resp);
    }

    //检查参数
    protected function check_params()
    {
        $this->jsdata = $this->request->param();

        if (isset($this->jsdata['rsn'])) {
            RequestContext::$sn = $this->jsdata['rsn'];
        }

        $what = null;
        $this->paramlist['robot_key'] = 'optstring';
        $this->paramlist['robot_id'] = 'optstring';
        $this->paramlist['whosyourdaddy'] = ['optstring', null];

        //city_id
        if (empty($this->jsdata['city_id']) && !empty($this->request->server('HTTP_CITY_ID'))){
            $this->jsdata['city_id'] = $this->request->server('HTTP_CITY_ID');
        }

        //token
        if (empty($this->jsdata['token']) && !empty($this->request->server('HTTP_TOKEN'))){
            $this->jsdata['token'] = $this->request->server('HTTP_TOKEN');
        }

        if (!isset($this->paramlist['token'])) {
            $this->paramlist['token'] = ['optstring', ''];
        }

        $this->argv = \common::check_json_keys($this->jsdata,$this->paramlist,$what);
        if (!$this->argv) {
            if (config('app.app_debug')){
                $msg = "invailed params,miss " . $what . "!";
            }
            else{
                $msg = "输入错误,请确认后重试！";
            }

            $this->throw_myexception(E_PARAMS_ERROR,$msg);
            //throw new BaseException(E_PARAMS_ERROR, $msg);
        }


        $this->argv['pos'] = 0;
        $this->argv['limit'] = 2000;
        if (isset($this->argv['pno'])) {
            $this->argv['pos'] = ($this->argv['pno'] - 1) * $this->argv['psize'];
            if ($this->argv['pos'] < 0) {
                $this->argv['pos'] = 0;
            }
            $this->argv['limit'] = $this->argv['psize'];
            if ($this->argv['limit'] < 0 || $this->argv['limit'] > 2000) {
                $this->argv['limit'] = 2000;
            }
        }
    }
}